#!/bin/bash
 
if [ ! -e ./backup_ifconfig ]; then
	echo "Haciendo backup de interfaces"
	ifconfig >backup_ifconfig
fi

if [ ! -e ./backup_route ]; then
	echo "Haciendo backup de rutas"
	ip route show all >backup_route
fi

if [ ! -e ./backup_hosts ]; then
	echo "Haciendo backup de /etc/hosts"
	cp /etc/hosts ./backup_hosts
fi

if [ ! -e ./backup_ip_forward ]; then
	echo "Haciendo backup de /proc/sys/net/ipv4/ip_forward"
	cp /proc/sys/net/ipv4/ip_forward ./backup_ip_forward
fi

echo "Actualizando /etc/hosts"
cp hosts /etc/hosts

echo "Reseteando interfaces y rutas"
ip route flush all

for placa in `ifconfig | grep -e "^eth0:[[:digit:]]" | cut -f1 -d" "`; do
	ifconfig $placa down
done

for placa in `ifconfig | grep -e "^eth" | cut -f1 -d" "`; do
	ifconfig $placa down
done

echo "Reseteando arp cache"
ip neigh flush dev eth0

echo "Matando pppd"
killall -9 pppd

if [[ `echo "$1" | sed -e "s/\(..\).*/\1/"` == "h1" ]]; then
	echo "Activando ip forward"
	echo "1" >/proc/sys/net/ipv4/ip_forward
else
	echo "Desactivando ip forward"
	echo "0" >/proc/sys/net/ipv4/ip_forward
fi

if [[ $2 = "modem" ]]; then
	if [[ $3 = "llamar" ]]; then
		comandoppp="pppd updetach noauth debug /dev/ttyS0 9600 nodefaultroute \
		connect \"/usr/sbin/chat -v -f `pwd`/llamar\" "
	elif [[ $3 = "atender" ]]; then
		comandoppp="pppd updetach noauth debug /dev/ttyS0 9600 nodefaultroute \connect \"/usr/sbin/chat -v -f `pwd`/atender\" "
	else
		echo "ERROR: SI SE UTILIZARA UN MODEM, SE DEBE ESPECIFICAR llamar O atender"
		exit 1
	fi
elif [[ $2 = "nullmodem" ]]; then
	comandoppp="pppd updetach nodefaultroute crtscts lock /dev/ttyS0 38400 local "
fi

echo "Todo limpiado. Ya puede configurar"
